<template>
  <div class="common-page page-policy-articles" v-loading.fullscreen="loading">
    <div class="common-page__head">
      <!-- <comp-paths class="route" :paths="['/policy', $route.fullPath]"></comp-paths> -->
      <div class="title">{{ $options.title }}</div>
    </div>
    <comp-sys-feedbacks class="common-page__body" ref="list" :type="type" select link>
      <template #head="scope">
        <el-button type="plain" size="small" icon="el-icon-refresh" @click="search()">刷新列表</el-button>
      </template>
      <template #body="scope">
        <el-button type="primary" size="mini" @click.stop="replyFeedback('start',scope.data)">{{
            scope.data.status ==0 ?'答复':'查看'
          }}</el-button>
      </template>
    </comp-sys-feedbacks>

    <comp-dialog title="答复" ref="reply">
      <el-form size="small" label-width="98px" style="margin-right:20px" @submit.native.prevent v-if="feedback">
        <el-form-item label="用户：" required>
          <el-input  v-model="feedback.userName" placeholder="请输入" resize="none" :readonly="true"></el-input>
        </el-form-item>
        <el-form-item label="部门：" required>
          <el-input  v-model="feedback.deptName" placeholder="请输入" resize="none" :readonly="true"></el-input>
        </el-form-item>
        <el-form-item label="反馈内容：" required>
          <el-input type="textarea" v-model="feedback.content" style="width: 640px" placeholder="请输入"
                    :maxlength="100" :rows="5" resize="none" :readonly="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="答复内容：" required>
          <el-input type="textarea" v-model="feedback.replyContent" style="width: 640px" placeholder="请输入"
                    :maxlength="100" :rows="5" resize="none" :readonly="loading"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-if="feedback.status == 0" type="primary" icon="el-icon-check" @click="replyFeedback('submit')">提交</el-button>
          <el-button type="plain" icon="el-icon-close" @click="replyFeedback('end')">放弃</el-button>
        </el-form-item>
      </el-form>
    </comp-dialog>
  </div>
</template>

<script>
import mixin from "../mixin";
import {string, int, toMagicImgURL, selectFile} from "../util";
import env from "@/env";

export default {
  name: "page-system-feedbacks",
  title: "系统反馈",
  nodes: ["/menu/system"],
  childs: [],
  mixins: [mixin],
  data: () => ({
    loading: false,
    type: "policy",
    article: null,
    feedback:null,
    tags: [],
    baseApiUrl: env.xhrPrefix,
  }),
  methods: {
    search() {
      if (this.loading) return;
      this.$nextTick(() => this.$refs.list.init());
    },
    destroy() {
      if (this.loading) return;
      return true;
    },
    replyFeedback(action, data){

      if (!action || action === "start") {
        this.feedback = data;
        this.$refs.reply.init();
      }else if(action == 'validate'){
        let form = this.feedback
        let error = ''
        error = error || form.replyContent.trim()  ? '' : '请输入答复内容！'

        if (error) this.plugins.modal.toast(error, "error");
        return !error
      }else if(action == 'submit'){
        if (!this.replyFeedback("validate")) return;
        this.loading = true;
        let form = this.feedback
        this.plugins.user.sysFeedbackReply(form).then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.plugins.modal.toast("答复成功！", "success");
            this.replyFeedback("end");
            this.search();
          } else {
            this.plugins.modal.alert("添加失败", res.code + "：" + res.msg, "error");
          }
        });
      }else if(action == 'end'){
        this.$refs.reply.close();
      }
    },
    init() {
      if (this.loading) return;
      this.loading = true;
      this.$root.$refs.navbar.init();
      this.plugins.user.call(async () => {
        let res = await this.plugins.user.init();
        if (res.code === 0) {
        }
        return res;
      }, async () => {
        if (this.access(this.$route.name)) return {code: 0};
        return this.env.xhrErrors.find((error) => error.code === 4);
      }).then((res) => {
        this.loading = false;
        this.$root.$refs.navbar.init();
        if (res.code === 0) {
          this.search();
        } else if (res.code === 2) {
          this.$root.$refs.navbar.login();
        } else {
          this.plugins.modal.alert("页面加载失败", res.code + "：" + res.msg, "error").then(() => this.$router.replace("/home"));
        }
      });
    },
  },
};
</script>

<style lang="scss">
.page-policy-articles {
  overflow: hidden;
}
</style>
